package chd.edu.spzx.manager.controller;

import chd.edu.spzx.manager.service.SysMenuService;
import chd.edu.spzx.manager.service.SysUserService;
import chd.edu.spzx.manager.service.ValidateCodeService;
import chd.edu.spzx.model.dto.system.LoginDto;
import chd.edu.spzx.model.entity.system.SysUser;
import chd.edu.spzx.model.vo.common.Result;
import chd.edu.spzx.model.vo.common.ResultCodeEnum;
import chd.edu.spzx.model.vo.system.LoginVo;
import chd.edu.spzx.model.vo.system.SysMenuVo;
import chd.edu.spzx.model.vo.system.ValidateCodeVo;
import chd.edu.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Zhang Chaoliang
 * create 2023
 * 登录、初始页面控制类
 */

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @Operation(summary = "登录的方法")
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 生成图片验证码
     * @return
     */
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

//    /**
//     * 获取当前登录用户
//     * @param
//     * @return
//     */
//    @GetMapping(value = "/getUserInfo")
//    public Result getUserInfo(@RequestHeader(name="token") String token){
////        //1 从请求头获取token
////        //getUserInfo(HttpServletRequest request)
////        String token = request.getHeader("token");
//
//        //2 根据token查询redis获取用户信息
//        SysUser sysUser = sysUserService.getUserInfo(token);
//
//        //3 用户信息返回
//        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
//    }

    /**
     * 获取当前登录用户（优化版）
     * @param
     * @return
     */
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo(@RequestHeader(name="token") String token){

        return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户退出
     * @return
     */
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(name="token") String token){
        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询用户可以操作的菜单
     * @return
     */
    @GetMapping("/menus")
    public Result menus(){
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findMenusByUserId();
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }


}
